﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;

public partial class Page_UserEdit : System.Web.UI.Page
{
    SystemFrameworkData SuperiorSFD;
    UserExData ued1;
    protected void Page_Load(object sender, EventArgs e)
    {
        string strid = Request.QueryString["id"];
        string strsid = Request.QueryString["sid"];
        Guid sid = Guid.Parse(strsid);
        ued1 = null;
        if (sid == Guid.Empty)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('发生未知错误');closewindow(); </script>");
        }
        else
        {
            SuperiorSFD = SystemFrameworkDataDBOption.SelectByID(sid);
            TextBox1.Text = SuperiorSFD.Name;
        }
        if (strid == null || strid == "")
        {
            Button4.Visible = false;
        }
        else
        {
            Guid id = Guid.Parse(strid);
            ued1 = UserExDataDBOption.SelectByUserID(id);
        }
        if(!IsPostBack)
        {
            if(ued1!=null)
            {
                TextBox2.Enabled = false;
                TextBox9.Visible = false;
                TextBox2.Text = ued1.UserName;
                TextBox3.Text = ued1.Contact;
                TextBox5.Text = ued1.Telephone;
                TextBox8.Text = ued1.Remark;
                if (ued1.Authority == "读写")
                    CheckBox1.Checked = true;
                else
                    CheckBox1.Checked = false;
                CheckBox2.Checked = ued1.IsAvailable;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MembershipUser user;
        if (ued1 == null)
        {
            try
            {
                user = Membership.CreateUser(TextBox2.Text, TextBox9.Text);
            }
            catch (MembershipCreateUserException ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加失败，错误原因为：" + ex.StatusCode.ToString() + "');</script>");
                return;
            }
        }
        else
            user = Membership.GetUser(TextBox2.Text);
        if (user.IsApproved != CheckBox2.Checked)
        {
            user.IsApproved = CheckBox2.Checked;
            Membership.UpdateUser(user);
        }
        UserExData ued = new UserExData();
        ued.UserID = (Guid)user.ProviderUserKey;
        ued.UserLevelID = SuperiorSFD.ID;
        ued.UserName = TextBox2.Text;
        ued.Contact = TextBox3.Text;
        ued.Telephone = TextBox5.Text;
        ued.Remark = TextBox8.Text;
        if (CheckBox1.Checked)
        {
            ued.Authority = "读写";
        }
        else
        {
            ued.Authority = "只读";
        }
        if (ued1 == null)
        {
            ued.CreateTime = DateTime.Now;
        }
        else
        {
            ued.CreateTime = ued1.CreateTime;
        }
        ued.IsAvailable = CheckBox2.Checked;
        ued.Creator = Membership.GetUser().UserName;
        ued.UpdateTime = DateTime.Now;
        if (ued1 == null)
        {
            UserExDataDBOption.Insert(ued);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加成功');GetDataAndClose(); </script>");
        }
        else
        {
            ued.UserID = ued1.UserID;
            UserExDataDBOption.Update(ued);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('修改成功');GetDataAndClose(); </script>");
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(TextBox2.Text);
        string p=user.ResetPassword();
        user.ChangePassword(p, "888888");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('重置密码成功'); </script>");
    }
}